//
//  AppDelegate.swift
//  JaimeCore
//
//  Created by seongbrave on 05/18/2017.
//  Copyright (c) 2017 seongbrave. All rights reserved.
//

import UIKit
import URLNavigator
import SwiftyUserDefaults
import UtilCore
import JonSnowCore
import JaimeCore
import RickonCore
import NetWorkCore
import BrandonCore

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window!.backgroundColor=UIColor.white
        setCommonAppleance()      //设置导航栏颜色
        setupURLNavigator(launchOptions) //App 路由配置
        setModule()               //模块配置
        setIQKeyboardManager()
        setupRootViewController()
        return true
    }
    
    func applicationWillResignActive(_ application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }
    
    func applicationDidEnterBackground(_ application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }
    
    func applicationWillEnterForeground(_ application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }
    
    func applicationDidBecomeActive(_ application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }
    
    func applicationWillTerminate(_ application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }
    
    
}
/// 基础配置
extension AppDelegate{
    internal func setupRootViewController() {
        let  firstPage = Defaults[.firstPage]
        /// 登陆成功后 点击了进入觅客商城按钮 则设置为true 否则为false
        ///表示第一次登陆
        if firstPage == false {
            window?.rootViewController =  JonSnowCore.guideVc
            self.window?.makeKeyAndVisible()
        }else {
            self.setTabBarVc()
            
        }
    }
    /// 设置tabbarcontroller
    internal func setTabBarVc() ->  Void{
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let testVc = storyboard.instantiateViewController(withIdentifier: "TestVc")
//        JaimeCore.raiseMallVc
        self.window?.rootViewController = testVc
    }
    /**
     跳转到登陆界面
     */
    internal func toLoginVc(){
        let loginNav = UINavigationController(rootViewController: JonSnowCore.loginVc)
        window!.rootViewController = loginNav
    }
    /**
     设置导航等 的主题颜色
     */
    internal func setCommonAppleance(){
        
        
        UIApplication.shared.statusBarStyle = .lightContent
        let navigationBarAppearance = UINavigationBar.appearance()
        //设置显示的颜色
        navigationBarAppearance.barTintColor = UIColor.white
        navigationBarAppearance.tintColor = UIColor.black
        
        let navTitleFont = UIFont(name: Theme.font.normal, size: 17)
        navigationBarAppearance.titleTextAttributes = [NSFontAttributeName: navTitleFont!, NSForegroundColorAttributeName:Theme.tf.text]
        if #available(iOS 10.0, *) {
            UITabBarItem.appearance().badgeColor = UIColor(hex: "e31436")
        } else {
            // Fallback on earlier versions
        }
        /// tabbar
        let tabBarAppearance = UITabBar.appearance()
        tabBarAppearance.tintColor = Theme.btn.normal
        tabBarAppearance.barTintColor = UIColor.white
        tabBarAppearance.isOpaque = true
    }
    /// 设置IQKeyboardManager配置
    internal func setIQKeyboardManager(){
        
    }
    /*
     模块化基础配置
     */
    internal func setModule() {
        Envs.isDebug = true
        setUtilCore()
        setNetCoreConfing()
    }
    
}
/// 第三方配置
extension AppDelegate{
    
    /*
     UtilCore基础配置
     */
    internal func setUtilCore()  {
        UtilCore.alertmsg = AppConfig.alert
        ///这块主要是图片封装的时候回用到图片地址
        UtilCore.sharedInstance.baseUrl = "http://10.0.0.222:9085"
    }
    /// 修改NetWorkCore 网络相关配置
    internal func setNetCoreConfing() {
        
        NetWorkCore.baseUrl = ["http://10.0.0.222:9085"]
        let devid = Defaults[.uuid]
        if devid.characters.count > 0 {
            UtilCore.sharedInstance.devid = devid
            NetWorkAPI.sharedInstance.headers["devid"] = devid
        }
        NetWorkAPI.sharedInstance.headers["apptype"] = "iOS"
        NetWorkAPI.sharedInstance.headers["appversion"] = getAppVersion()
    }
    /// 获取app 版本信息
    internal func getAppVersion() -> String {
        if let appversion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] {
            return (appversion as? String) ?? ""
        }else{
            return ""
        }
    }
    ///app路由注册
    internal func setupURLNavigator(_ launchOptions: [UIApplicationLaunchOptionsKey: Any]?){
        Navigator.scheme = AppConfig.appScheme
        
        MainNavigatorMap.initialize()
        if let url = launchOptions {
            
            print(url)
        }
    }
    
}
